<?php

namespace app\admin\controller;

use app\api\model\Privatechat;
use think\Db;

class Group extends Common
{
    public function list()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            if (in_array($search_type, ['id', 'yqr'])) {
                $where[$search_type] = $search_name;
            } else {
                $where[$search_type] = ['like', '%' . $search_name . '%'];
            }
        }

        $data = Db::name('share_group')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()])->each(function ($item, $key) {
            return $item;
        });

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function add()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if (!$id) {
                if (!isset($post['name']) || !$post['name']) {
                    return ['err' => '名称不能为空'];
                }
                $post['create_time'] = time();
                $db = Db::name('share_group')->insert($post);

                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                if (!isset($post['name']) || !$post['name']) {
                    return ['err' => '名称不能为空'];
                }

                $post['update_time'] = time();
                $db = Db::name('share_group')->where('id', $id)->update($post);
                if ($db !== false) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('share_group')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }
        return $data;
    }
	public function dele()
    {
        if (request()->isAjax()) {

            $id = input('id');
            if ($id) {
                $db = Db::name('share_group')->delete($id);
                if ($db) {
                	Db::name('share_group_join')->where(['group_id'=>$id])->delete();
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
	public function member()
	{
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            if (in_array($search_type, ['group_id', 'user_id'])) {
                $where[$search_type] = $search_name;
            }
        }

        $data = Db::name('share_group_join')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()])->each(function ($item, $key) {
        	$user = _user_cache($item['user_id']);
        	$item['user_name'] = $user['name'];
            return $item;
        });

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }
	public function kickout()
	{
        if (request()->isAjax()) {

            $id = input('id');
            if ($id) {
            	$joininfo = Db::name('share_group_join')->where(['id'=>$id])->find();
            	$db = Db::name('share_group_join')->where(['id'=>$id])->update(['state'=>-3,'exit_time'=>time()]);
                if ($db) {
                	Db::name('share_group')->where(['id'=>$joininfo['group_id']])->setDec('count');

                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
	}
}